﻿using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Configure_Mini_0._1;
using System.Text.RegularExpressions;

namespace Configure_Mini_0._1
{
    public partial class Uiioserviceconfiguration : Form
    {
        // creat new instance
        SerialPort sp1 = new SerialPort();

        public Uiioserviceconfiguration()
        {
            InitializeComponent();
        }

        private void Uiioserviceconfiguration_Load(object sender, EventArgs e)
        {
            Configure_Mini_0._1.Profile.LoadProfile(); //Load

            //pre-set baudrate index;
            switch (Profile.G_BAUDRATE)
            {
                case "300":
                    cbBaudRate.SelectedIndex = 0;
                    break;
                case "600":
                    cbBaudRate.SelectedIndex = 1;
                    break;
                case "1200":
                    cbBaudRate.SelectedIndex = 2;
                    break;
                case "2400":
                    cbBaudRate.SelectedIndex = 3;
                    break;
                case "4800":
                    cbBaudRate.SelectedIndex = 4;
                    break;
                case "9600":
                    cbBaudRate.SelectedIndex = 5;
                    break;
                case "19200":
                    cbBaudRate.SelectedIndex = 6;
                    break;
                case "38400":
                    cbBaudRate.SelectedIndex = 7;
                    break;
                case "115200":
                    cbBaudRate.SelectedIndex = 8;
                    break;
                default:
                    {
                        MessageBox.Show("Error: Baud Rate Index Setting Error.", "Error");
                        return;
                    }
            }

            //pre-set baudrate index;
            switch (Profile.G_DATABITS)
            {
                case "5":
                    cbDataBits.SelectedIndex = 0;
                    break;
                case "6":
                    cbDataBits.SelectedIndex = 1;
                    break;
                case "7":
                    cbDataBits.SelectedIndex = 2;
                    break;
                case "8":
                    cbDataBits.SelectedIndex = 3;
                    break;
                default:
                    {
                        MessageBox.Show("Error: Data Bits Index Setting Error.", "Error");
                        return;
                    }
            }

            //pre-set stop index;
            switch (Profile.G_STOP)
            {
                case "1":
                    cbStop.SelectedIndex = 0;
                    break;
                case "1.5":
                    cbStop.SelectedIndex = 1;
                    break;
                case "2":
                    cbStop.SelectedIndex = 2;
                    break;
                default:
                    {
                        MessageBox.Show("Error: Stop Index Setting Error.", "Error");
                        return;
                    }
            }

            //pre-set parity index;
            switch (Profile.G_PARITY)
            {
                case "NONE":
                    cbParity.SelectedIndex = 0;
                    break;
                case "ODD":
                    cbParity.SelectedIndex = 1;
                    break;
                case "EVEN":
                    cbParity.SelectedIndex = 2;
                    break;
                default:
                    {
                        MessageBox.Show("Error: Parity Index Setting Error.", "Error");
                        return;
                    }
            }


            //Serial Input Setup Check
            string[] str = SerialPort.GetPortNames();
            if (str == null)
            {
                MessageBox.Show("Can't Find Serial Input", "Error");
                return;
            }

            //Add Serial Items
            foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
            {
                cbSerial.Items.Add(s);
            }

            cbBaudRate.SelectedIndex = 5;
            cbDataBits.SelectedIndex = 3;
            cbStop.SelectedIndex = 0;
            cbParity.SelectedIndex = 0;

            // ?? Control.CheckForIllegalCrossThreadCalls = false;    
            // ?? 这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,，不允许在winform中直接跨线程访问控件的属性)

            cbHexRecieve.Checked = true;
            cbHexSend.Checked = true;

        }

        // Set Timer
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.tsslDate.Text = DateTime.Now.ToString();
        }
    }
}
